package com.qingmeng.outsourcing.portal.controller;

import com.github.pagehelper.PageHelper;
import com.qingmeng.outsourcing.common.api.CommonPage;
import com.qingmeng.outsourcing.common.api.CommonResult;
import com.qingmeng.outsourcing.common.utils.FileUtil;
import com.qingmeng.outsourcing.portal.model.PortalUserDetails;
import com.qingmeng.outsourcing.portal.model.dto.ProjectPublishParam;
import com.qingmeng.outsourcing.portal.model.vo.ProjectListVo;
import com.qingmeng.outsourcing.portal.service.ProjectService;
import com.qingmeng.outsourcing.portal.service.impl.ProjectServiceImpl;
import com.qingmeng.outsourcing.portal.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping(value = "/project")
@Api(tags = "ProjectController")
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    @ApiOperation("发布项目")
    @PostMapping(value = "/publishTask")
    public CommonResult<Integer> publishTask(@ApiParam("发布项目时填写的项目信息") @RequestBody ProjectPublishParam param){
        int result = projectService.publishTask(param);
        if(result > 0){
            return CommonResult.success(result,"发布项目成功");
        }
        return CommonResult.failed("发布项目失败");
    }

    @ApiOperation("上传项目相关文件接口")
    @PostMapping(value = "/uploadFile")
    public CommonResult<String> uploadFile(@ApiParam("项目相关文件") @RequestParam("file") MultipartFile file)throws Exception {
        Long userId = SecurityUtils.getCurrentUser().getId();
        String filePath = FileUtil.portalUploadFileByUserId(userId,file,2);
        if(filePath != null){
            return CommonResult.success(filePath,"上传文件成功");
        }
        return CommonResult.failed("上传文件失败，请重试");
    }

    @ApiOperation("获取任务大厅所需的项目列表")
    @GetMapping(value = "/list")  //不需要往数据库里存 用GetMapping     如果与数据库有交互的就需要用 PostMapping
    public CommonResult<CommonPage<ProjectListVo>> list(@ApiParam("页码") @RequestParam(value = "pageNum",/*默认值*/defaultValue = "1") Integer pageNum,
                                                        @ApiParam("每页包含的条数") @RequestParam(value = "pageSize",/*默认值*/defaultValue = "5") Integer pageSize){
        List<ProjectListVo> projectListVoList = projectService.list(pageNum,pageSize);
        if (projectListVoList != null && projectListVoList.size() > 0){
            return CommonResult.success(CommonPage.restPage(projectListVoList),"获取成功");
        }
        return CommonResult.failed("没有查到相关数据");
    }
}
